package com.foo.web.controller.wechat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.foo.wechat.domain.WxUser;
import com.foo.wechat.service.IWxUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.foo.common.annotation.Log;
import com.foo.common.enums.BusinessType;
import com.foo.wechat.domain.WxFarm;
import com.foo.wechat.service.IWxFarmService;
import com.foo.common.core.controller.BaseController;
import com.foo.common.core.domain.AjaxResult;
import com.foo.common.utils.poi.ExcelUtil;
import com.foo.common.core.page.TableDataInfo;

/**
 * 农场管理Controller
 *
 * @author foo
 * @date 2025-11-10
 */
@Controller
@RequestMapping("/wechat/WxFarm")
public class WxFarmController extends BaseController
{
    private String prefix = "wechat/WxFarm";

    @Autowired
    private IWxFarmService wxFarmService;

    @Autowired
    private IWxUserService wxUserService;

    @RequiresPermissions("wechat:WxFarm:view")
    @GetMapping()
    public String WxFarm()
    {
        return prefix + "/WxFarm";
    }

    /**
     * 查询农场管理列表
     */
    @RequiresPermissions("wechat:WxFarm:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WxFarm wxFarm)
    {
        startPage();
        List<WxFarm> list = wxFarmService.selectWxFarmList(wxFarm);
        return getDataTable(list);
    }


    @GetMapping("/listAjax")
    @ResponseBody
    public AjaxResult listAjax(WxFarm wxFarm)
    {
        List<WxFarm> list = wxFarmService.selectWxFarmList(wxFarm);
        return AjaxResult.success (list);
    }



    /**
     * 新增农场管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存农场管理
     */
    @RequiresPermissions("wechat:WxFarm:add")
    @Log(title = "农场管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WxFarm wxFarm)
    {
        WxUser wxUser = wxUserService.selectWxUserById(wxFarm.getFarmUid());
        List<String> list = new ArrayList<>(Arrays.asList(wxUser.getUserType().split(",")));
        String target = "1";
        if (!list.contains(target)) {
            list.add(target);
            wxUser.setUserType(String.join(",", list));
            wxUserService.updateWxUser(wxUser);
        }else {
            return error("该用户已有绑定的农场！");
        }
        return toAjax(wxFarmService.insertWxFarm(wxFarm));
    }

    /**
     * 修改农场管理
     */
    @GetMapping("/edit/{farmId}")
    public String edit(@PathVariable("farmId") Long farmId, ModelMap mmap)
    {
        WxFarm wxFarm = wxFarmService.selectWxFarmById(farmId);
        mmap.put("wxFarm", wxFarm);
        return prefix + "/edit";
    }

    /**
     * 修改保存农场管理
     */
    @RequiresPermissions("wechat:WxFarm:edit")
    @Log(title = "农场管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WxFarm wxFarm)
    {
        WxFarm wxFarm1 = wxFarmService.selectWxFarmById(wxFarm.getFarmId());
        if(!wxFarm1.getFarmUid().equals(wxFarm.getFarmUid())){
            WxUser wxUser1 = wxUserService.selectWxUserById(wxFarm.getFarmUid());
            List<String> list = new ArrayList<>(Arrays.asList(wxUser1.getUserType().split(",")));
            String target = "1";
            if (!list.contains(target)) {
                list.add(target);
                wxUser1.setUserType(String.join(",", list));
                wxUserService.updateWxUser(wxUser1);
                WxUser wxUser2 = wxUserService.selectWxUserById(wxFarm1.getFarmUid());
                List<String> list1 = new ArrayList<>(Arrays.asList(wxUser2.getUserType().split(",")));
                list1.remove("1");
                wxUser2.setUserType(String.join(",", list1));
                wxUserService.updateWxUser(wxUser2);
            }else {
                return error("该用户已有绑定的农场！");
            }
        }


        return toAjax(wxFarmService.updateWxFarm(wxFarm));
    }

    /**
     * 删除农场管理
     */
    @RequiresPermissions("wechat:WxFarm:remove")
    @Log(title = "农场管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        WxFarm wxFarm = wxFarmService.selectWxFarmById(Long.parseLong(ids));
        WxUser wxUser = wxUserService.selectWxUserById(wxFarm.getFarmUid());
        List<String> list1 = new ArrayList<>(Arrays.asList(wxUser.getUserType().split(",")));
        list1.remove("1");
        wxUser.setUserType(String.join(",", list1));
        wxUserService.updateWxUser(wxUser);

        return toAjax(wxFarmService.deleteWxFarmByIds(ids));
    }
}
